<template>
  <el-container id="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img :src="require('@/assets/paidaxing.jpg')" alt />
        <span>雪碧商城后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iScollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggle_Collapse">|||</div>
        <!-- 侧边栏菜单区 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="iScollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path, item.authName, subItem.authName)"
            >
              <!-- 一级菜单模板区 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右侧内容主体 -->
      <el-main width="200px">
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right" v-if="$route.path !== '/welcome'">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{activeBreadcrumb.firstAuthName}}</el-breadcrumb-item>
          <el-breadcrumb-item>{{activeBreadcrumb.secondAuthName}}</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 路由占位 -->
        <router-view />
      </el-main>
    </el-container>

    <!-- 一键放回顶部 -->
    <el-backtop target="#home_container" :bottom="100" :right="50" :visibility-height="300">
      <img :src="require('@/assets/BackTop.png')" style="width: 100%;" />
    </el-backtop>
  </el-container>
</template>


<script>
export default {
  name: 'home',
  data() {
    return {
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-yonghus',
        '103': 'iconfont icon-ic_opt_feature',
        '101': 'iconfont icon-shangpin-tianchong',
        '102': 'iconfont icon-dingdan',
        '145': 'iconfont icon-shuju'
      },
      // 是否折叠
      iScollapse: false,
      // 被激活的  路由链接地址
      activePath: '',
      // 面包屑导航
      activeBreadcrumb: ''
    }
  },
  created() {
    this.getMenuList(),
      /* 保存 选中列表 */
      (this.activePath = window.sessionStorage.getItem('activePath')),
      (this.activeBreadcrumb = JSON.parse(
        sessionStorage.getItem('activeBreadcrumb')
      ))
  },
  mounted() {
    // 页面加载后判断  设备
    let flag = navigator.userAgent.match(
      /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
    )
    //  如果是手机  把左侧侧边栏收起来
    if (flag) {
      this.iScollapse = true
      // 设置标准字体：
      document.getElementsByTagName('html')[0].style.fontSize = '12px'
    } else {
      this.iScollapse = false
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data } = await this.$axios.get('menus')
      // console.log(data)
      if (data.meta.status !== 200) return this.$message.error(data.meta.msg)
      this.menuList = data.data
    },
    // 点击按钮 切换菜单折叠
    toggle_Collapse() {
      this.iScollapse = !this.iScollapse
    },
    saveNavState(activePath, firstAuthName, secondAuthName) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath

      // 将选中的类列表赋值给 面包屑
      this.activeBreadcrumb = { firstAuthName, secondAuthName }
      window.sessionStorage.setItem(
        'activeBreadcrumb',
        JSON.stringify({ firstAuthName, secondAuthName })
      )
      // console.log(this.activeBreadcrumb)
    }
  }
}
</script>

<style lang="less" scoped>
#home_container {
  font-size: 20px !important;
  width: 100%;
  height: 100%;
  /* 一键返回顶部 组件的坑： 1、需要设置 最大 宽度 和 高度 */
  /* 2、el-backtop绑定的盒子，需要一个滚动样式：overflow-y: scroll; */
  overflow-y: scroll;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  > div {
    display: flex;
    align-items: center;
    img {
      height: 56px;
      width: 56px;
      border-radius: 50%;
      margin: 2px;
    }
    span {
      font: 1.3rem Tahoma, Helvetica, Arial, '\5b8b\4f53', sans-serif;
      color: #fffecc;
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #33374a;
  /* 解决 el-menu 右边有 1 px 空白的bug */
  .el-menu {
    // width: 100%;
    border-right: none;
  }
  .toggle-button {
    background-color: #4a5064;
    font-size: 0.7rem;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2rem;
    cursor: ew-resize;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
</style>
